<?php
/***************************************************************************
 *                              pilot_update.php
 *                            -------------------
 *                       (APU) Automated Pilot Utility
 *   begin                : Sunday, Dec 5, 2004
 *   copyright            : (C) 2004 SF Games
 *   email                : techsupport@sf-games.com
 *
 *   Expects the following POST variables:
 *
 *			  Pilot ID: pilotid
 *     Change Password: changepass		- Indicates reset to default password
 *        Delete Pilot: delete_pilot    - Indicates to delete pilot record
 *          First Name: firstname       -
 *           Last Name: lastname        -
 *              E-Mail: email			-
 *           Join Date: joindate        -
 *       Last Fly Date: lastflydate     -
 *            A/C Rank: ac_rank			- When changing ranks, ensure flight hours match
 *           Helo Rank: helo_rank		-
 *          A/C Hourse: ac_hours		-
 *          Helo Hours: helo_hours		-
 *         Total Hours: total_hours		-
 *                 Bid: bid				- Bid, route
 *                Dart: dart			- Has flown a DART mission
 *                 SAR: sar				- Has flown a SAR mission
 *       Fleet Support: fleet_support	- Has flown Fleet Support mission
 *    Leave of Absence: loa				- Pilot is on a leave of absence
 *              Active: active			- Pilot is Active
 ***************************************************************************/

	$apus_root_path = '../include/';
	include($apus_root_path . 'common.php');
	include($apus_root_path . 'config.php');

	// Get the posted variables
	$pilotid       = $_POST['pilotid'];
	$changepass    = $_POST['changepass'];
	$deletePilot   = $_POST['delete_pilot'];
	$first_name    = $_POST['firstname'];
	$last_name     = $_POST['lastname'];
	$email         = $_POST['email'];
	$join_date     = $_POST['joindate'];
	$last_fly_date = $_POST['lastflydate'];
	$ac_rank       = $_POST['ac_rank'];
	$helo_rank     = $_POST['helo_rank'];
	$ac_hours      = $_POST['ac_hours'];
	$helo_hours    = $_POST['helo_hours'];
	$total_hours   = $_POST['total_hours'];
	$bid           = $_POST['bid'];
	$dart          = $_POST['dart'];
	$sar           = $_POST['sar'];
	$fleet_support = $_POST['fleet_support'];
	$loa           = $_POST['loa'];
	$active        = $_POST['active'];

	// Fix the freakin checkbox crap
	if ( $changepass == "" )    $changepass    = "0"; else $changepass    = "1";
	if ( $deletePilot == "" )   $deletePilot   = "0"; else $deletePilot   = "1";
	if ( $dart == "" )          $dart          = "0"; else $dart          = "1";
	if ( $sar == "" )           $sar           = "0"; else $sar           = "1";
	if ( $fleet_support == "" ) $fleet_support = "0"; else $fleet_support = "1";
	if ( $loa == "" )           $loa           = "0"; else $loa           = "1";
	if ( $active == "" )        $active        = "0"; else $active        = "1";

	// HTML header
	echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
	echo "<html>\n";
	echo "<head>\n";
	echo "<title>APU Pilot Administration</title>\n";
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
	echo "</head>\n";
	echo "<body>\n";
	echo "<div align=\"center\">\n";
	echo("<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"4\" color=\"#990000\"><b>(APU) Automated Pilot Utility<br>Updating Pilot Record</b></font><br><br>\n");

	//
	// Attempt connection to mySQL
	//

	$db = mysql_connect($dbhost, $dbuname, $dbpass)
		or die("Could not connect to the SQL server<br>");
	mysql_select_db($dbname);

	// Check for password reset
	if ( $changepass == 1 )
	{
		// Encrypt the password
		$pencrpyt = md5($pilotDefaultPass);

		// SQL Update the password
		$query = "UPDATE " .$table_prefix ."PILOTS SET PASSWORD='$pencrpyt' WHERE PILOT_ID = " .sqlfix($pilotid);

		// Perform the update query
		$result = mysql_query($query);
		if ( !$result )
			die("Updating of PILOTS record failed.<br>Click your browsers back button to try again.<br>Failed with mySQL error: " . mysql_error());

		echo("<div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\" color=\"#990000\">Password reset to " .$pilotDefaultPass ." for Pilot ID: " .$pilotid ." Was successful<br>Press the back button to return to the administration page.</font></div><br>\n");
	}
	else if ( $deletePilot == 1 )
	{
		// SQL Update the given post vars to given pilotid
		$query = "UPDATE " .$table_prefix ."PILOTS SET DELETED='1' WHERE PILOT_ID = " .sqlfix($pilotid);

		// Perform the update query
		$result = mysql_query($query);
		if ( !$result )
			die("Updating of PILOTS record failed.<br>Click your browsers back button to try again.<br>Failed with mySQL error: " . mysql_error());

		echo("<div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\" color=\"#990000\">Update of Pilot ID: " .$pilotid ." Was successful<br>Pilot is now marked deleted<br>Press the back button to return to the administration page.</font></div><br>\n");
	}
	else
	{
		// SQL Update the given post vars to given pilotid
		$query = "UPDATE " .$table_prefix ."PILOTS SET FIRST_NAME='$first_name', LAST_NAME='$last_name', EMAIL='$email', RANK_AC='$ac_rank', RANK_HELO='$helo_rank', HOURS_AC='$ac_hours', HOURS_HELO='$helo_hours', TOTAL_HRS='$total_hours', BID='$bid', DART='$dart', SAR='$sar', FLEET_SUP='$fleet_support', LOA='$loa', ACTIVE='$active', JOINED='$join_date', LAST_FLY='$last_fly_date', DELETED='0' WHERE PILOT_ID = " .sqlfix($pilotid);

		// Perform the update query
		$result = mysql_query($query);
		if ( !$result )
			die("Updating of PILOTS record failed.<br>Click your browsers back button to try again.<br>Failed with mySQL error: " . mysql_error());

		echo("<div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"3\" color=\"#990000\">Update of Pilot ID: " .$pilotid ." Was successful<br>Press your browsers back button to return.</font></div><br>\n");
	}

	// HTML footer
	echo("<div align=\"center\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#990000\">" .$APU_COPYRIGHT ."</font></div><br>\n");
	echo "</div>\n";
	echo "</body>\n";
	echo "</html>\n";

?>
